import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/login/login.dart';
import 'package:kkhousekeep/page/login/login_controller.dart';
import 'package:kkhousekeep/page/me/set_password_page.dart';
import 'package:kkhousekeep/page/me/setting_controller.dart';
import 'package:kkhousekeep/tools/get_dialog_ext.dart';
import 'package:kkhousekeep/tools/shared_preferences.dart';
import 'package:kkhousekeep/widget/setting_section.dart';

import '../../widget/custom_app_bar.dart';

class Setting extends StatelessWidget {
  const Setting({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SettingController>(
      init: SettingController(),
      builder: (controller) => Scaffold(
        appBar: CustomAppBar("设置"),
        body: Container(
          padding: EdgeInsets.only(top: 32.h),
          color: const Color(0xFFf4f5f6),
          child: SafeArea(
            left: false,
            right: false,
            child: Obx(
              () => Column(
                children: [
                  _buildFacePhoto(controller),
                  (controller.userType.value == UserType.property)
                      ? _buildSetPassword(controller)
                      : Container(),
                  SizedBox(height: 32.h),
                  _buildLogOut(context, controller)
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildFacePhoto(SettingController controller) {
    return SettingSectionItem(
      title: "人脸照片",
      onTap: () {
        controller.buildFacePage();
      },
    );
  }

  Widget _buildSetPassword(SettingController controller) {
    return SettingSectionItem(
      title: "设置密码",
      onTap: () {
        Get.to(() => const SetPasswordPage());
      },
    );
  }

  Widget _buildLogOut(BuildContext context, SettingController controller) {
    return InkWell(
      onTap: () {
        Get.showCupertinoDialog(
          content: '是否退出当前帐号',
          rightStyle: const TextStyle(color: Color(0xFFDD5045)),
          onRightTap: () async {
            var phone = await getSp().getPhone();
            await getSp().clear();
            await getSp().setPhone(phone);
            Get.offAll(() => const LoginPage());
          },
        );
      },
      child: Container(
        height: 96.h,
        color: Colors.white,
        width: MediaQuery.of(context).size.width,
        alignment: Alignment.center,
        child: Text(
          "退出登录",
          style: TextStyle(
              color: const Color(0xFFDD5045),
              backgroundColor: Colors.white,
              fontSize: 32.sp),
        ),
      ),
    );
  }
}
